.TH std::basic_string::begin, 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_string::begin, \- std::basic_string::begin,

.SH Synopsis

   iterator begin();                       \fB(1)\fP (noexcept since C++11)
                                               (constexpr since C++20)
   const_iterator begin() const;           \fB(2)\fP (noexcept since C++11)
                                               (constexpr since C++20)
   const_iterator cbegin() const noexcept; \fB(3)\fP \fI(since C++11)\fP
                                               (constexpr since C++20)

   Returns an iterator to the first character of the string.

   begin() returns a mutable or constant iterator, depending on the constness of *this.

   cbegin() always returns a constant iterator. It is equivalent to const_cast<const
   basic_string&>(*this).begin().

   range-begin-end.svg

.SH Parameters

   \fI(none)\fP

.SH Return value

   Iterator to the first character.

.SH Complexity

   Constant.

.SH Notes

   libc++ backports cbegin() to C++98 mode.

.SH Example


// Run this code

 #include <iostream>
 #include <string>

 int main()
 {
     std::string s("Exemplar");
     *s.begin() = 'e';
     std::cout << s << '\\n';

     auto i = s.cbegin();
     std::cout << *i << '\\n';
 //  *i = 'E'; // error: i is a constant iterator
 }

.SH Output:

 exemplar
 e

.SH See also

   end     returns an iterator to the end
   cend    \fI(public member function)\fP
   \fI(C++11)\fP
   begin   returns an iterator to the beginning
   cbegin  \fI(public member function of std::basic_string_view<CharT,Traits>)\fP
